Conditions | 1 |
Paths | 1 |
Total Lines | 211 |
Code Lines | 170 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const db = require("../db/database.js"); |
||
6 | const copier = (function () { |
||
7 | const copyApiKey = config.copyApiKey; |
||
8 | |||
9 | function copyAll(res, apiKey) { |
||
10 | let sql = "INSERT INTO products" + |
||
11 | " (productId," + |
||
12 | " articleNumber," + |
||
13 | " productName," + |
||
14 | " productDescription," + |
||
15 | " productSpecifiers," + |
||
16 | " stock," + |
||
17 | " location," + |
||
18 | " price," + |
||
19 | " apiKey)" + |
||
20 | " SELECT productId," + |
||
21 | " articleNumber," + |
||
22 | " productName," + |
||
23 | " productDescription," + |
||
24 | " productSpecifiers," + |
||
25 | " stock," + |
||
26 | " location, " + |
||
27 | " price," + |
||
28 | "'" + apiKey + "'" + |
||
29 | " FROM products" + |
||
30 | " WHERE apiKey = ?"; |
||
31 | |||
32 | db.run(sql, copyApiKey, (err) => { |
||
33 | if (err) { |
||
34 | return res.status(500).json({ |
||
35 | errors: { |
||
36 | status: 500, |
||
37 | source: "/copy_products", |
||
38 | title: "Database error", |
||
39 | detail: err.message |
||
40 | } |
||
41 | }); |
||
42 | } else { |
||
|
|||
43 | let sql = "INSERT INTO orders" + |
||
44 | " (orderId," + |
||
45 | " customerName," + |
||
46 | " customerAddress," + |
||
47 | " customerZip," + |
||
48 | " customerCity," + |
||
49 | " customerCountry," + |
||
50 | " statusId," + |
||
51 | " apiKey)" + |
||
52 | " SELECT orderId," + |
||
53 | " customerName," + |
||
54 | " customerAddress," + |
||
55 | " customerZip," + |
||
56 | " customerCity," + |
||
57 | " customerCountry," + |
||
58 | " statusId," + |
||
59 | "'" + apiKey + "'" + |
||
60 | " FROM orders" + |
||
61 | " WHERE apiKey = ?"; |
||
62 | |||
63 | db.run(sql, copyApiKey, (err) => { |
||
64 | if (err) { |
||
65 | return res.status(500).json({ |
||
66 | errors: { |
||
67 | status: 500, |
||
68 | source: "/copy_orders", |
||
69 | title: "Database error", |
||
70 | detail: err.message |
||
71 | } |
||
72 | }); |
||
73 | } else { |
||
74 | let orderItemsSQL = "INSERT INTO order_items" + |
||
75 | " (orderId," + |
||
76 | " productId," + |
||
77 | " amount," + |
||
78 | " apiKey)" + |
||
79 | " SELECT orderId," + |
||
80 | " productId," + |
||
81 | " amount," + |
||
82 | "'" + apiKey + "'" + |
||
83 | " FROM order_items" + |
||
84 | " WHERE apiKey = ?"; |
||
85 | |||
86 | db.run(orderItemsSQL, copyApiKey, (err) => { |
||
87 | if (err) { |
||
88 | return res.status(500).json({ |
||
89 | errors: { |
||
90 | status: 500, |
||
91 | source: "/copy_orders", |
||
92 | title: "Database error in order_items", |
||
93 | detail: err.message |
||
94 | } |
||
95 | }); |
||
96 | } else { |
||
97 | return res.status(201).json({ |
||
98 | data: { |
||
99 | message: "Products and orders have been copied" |
||
100 | } |
||
101 | }); |
||
102 | } |
||
103 | }); |
||
104 | } |
||
105 | }); |
||
106 | } |
||
107 | }); |
||
108 | } |
||
109 | |||
110 | function copyProducts(res, apiKey) { |
||
111 | let sql = "INSERT INTO products" + |
||
112 | " (productId," + |
||
113 | " articleNumber," + |
||
114 | " productName," + |
||
115 | " productDescription," + |
||
116 | " productSpecifiers," + |
||
117 | " stock," + |
||
118 | " location," + |
||
119 | " price," + |
||
120 | " apiKey)" + |
||
121 | " SELECT productId," + |
||
122 | " articleNumber," + |
||
123 | " productName," + |
||
124 | " productDescription," + |
||
125 | " productSpecifiers," + |
||
126 | " stock," + |
||
127 | " location, " + |
||
128 | " price," + |
||
129 | "'" + apiKey + "'" + |
||
130 | " FROM products" + |
||
131 | " WHERE apiKey = ?"; |
||
132 | |||
133 | db.run(sql, copyApiKey, (err) => { |
||
134 | if (err) { |
||
135 | return res.status(500).json({ |
||
136 | errors: { |
||
137 | status: 500, |
||
138 | source: "/copy_products", |
||
139 | title: "Database error", |
||
140 | detail: err.message |
||
141 | } |
||
142 | }); |
||
143 | } else { |
||
144 | products.getAllProducts(res, apiKey, 201); |
||
145 | } |
||
146 | }); |
||
147 | } |
||
148 | |||
149 | function copyOrders(res, apiKey) { |
||
150 | let sql = "INSERT INTO orders" + |
||
151 | " (orderId," + |
||
152 | " customerName," + |
||
153 | " customerAddress," + |
||
154 | " customerZip," + |
||
155 | " customerCity," + |
||
156 | " customerCountry," + |
||
157 | " statusId," + |
||
158 | " apiKey)" + |
||
159 | " SELECT orderId," + |
||
160 | " customerName," + |
||
161 | " customerAddress," + |
||
162 | " customerZip," + |
||
163 | " customerCity," + |
||
164 | " customerCountry," + |
||
165 | " statusId," + |
||
166 | "'" + apiKey + "'" + |
||
167 | " FROM orders" + |
||
168 | " WHERE apiKey = ?"; |
||
169 | |||
170 | db.run(sql, copyApiKey, (err) => { |
||
171 | if (err) { |
||
172 | return res.status(500).json({ |
||
173 | errors: { |
||
174 | status: 500, |
||
175 | source: "/copy_orders", |
||
176 | title: "Database error", |
||
177 | detail: err.message |
||
178 | } |
||
179 | }); |
||
180 | } else { |
||
181 | let orderItemsSQL = "INSERT INTO order_items" + |
||
182 | " (orderId," + |
||
183 | " productId," + |
||
184 | " amount," + |
||
185 | " apiKey)" + |
||
186 | " SELECT orderId," + |
||
187 | " productId," + |
||
188 | " amount," + |
||
189 | "'" + apiKey + "'" + |
||
190 | " FROM order_items" + |
||
191 | " WHERE apiKey = ?"; |
||
192 | |||
193 | db.run(orderItemsSQL, copyApiKey, (err) => { |
||
194 | if (err) { |
||
195 | return res.status(500).json({ |
||
196 | errors: { |
||
197 | status: 500, |
||
198 | source: "/copy_orders", |
||
199 | title: "Database error in order_items", |
||
200 | detail: err.message |
||
201 | } |
||
202 | }); |
||
203 | } else { |
||
204 | orders.getAllOrders(res, apiKey, 201); |
||
205 | } |
||
206 | }); |
||
207 | } |
||
208 | }); |
||
209 | } |
||
210 | |||
211 | return { |
||
212 | copyAll: copyAll, |
||
213 | copyProducts: copyProducts, |
||
214 | copyOrders: copyOrders, |
||
215 | }; |
||
216 | }()); |
||
217 | |||
219 |